<template>
  <van-nav-bar title="我的订单" left-text="返回" left-arrow @click-left="goBack" fixed placeholder	safe-area-inset-top	/>
  <van-tabs v-model:active="active" sticky offset-top="45px" @click-tab="getorderData">
    <van-tab v-for="item in status" :title="item.title" :name="item.id">
      <van-card
        v-for="items in orderdata"
        :key="items.id"
        :price="items.amount"
        :title="'订单号：' + items.code"
        :desc="'商品名称：' + items.info.product.name"
        :thumb="items.info.product.thumbs_text"
      >
        <template #tags>
          <div>下单时间：{{ items.createtime }}</div>
        </template>
        <template #num>
          <van-tag plain type="danger">{{ items.status_text }}</van-tag>
        </template>
        <template #footer>
          <van-button size="mini" type="primary" @click="toInfoPage(items.id)"
            >订单详情</van-button
          >
          <van-button v-if="items.status == 0" size="mini" type="success" @click="ConfirmPayment(items.id)"
            >确认支付</van-button
          >
          <van-button
            v-if="items.expresscode"
            size="mini"
            type="warning"
            @click="toExpressPage(items.id)"
            >物流信息</van-button
          >
          <van-button
            size="mini"
            v-if="items.status == 2"
            type="success"
            @click="Confirmreceipt(items.id)"
            >确认收货</van-button
          >
          <van-button
            size="mini"
            v-if="items.status == 4"
            type="success"
            @click="toEvaluationPage(items.id)"
            >评价详情</van-button
          >
          <van-button
            size="mini"
            v-if="items.status > 0 && items.status != 4"
            type="danger"
            @click="rejected(items.id)"
            >申请退货</van-button>

            <van-button
              size="mini"
              v-if="items.status == '-3' && items.depot_status == false"
              type="danger"
              @click="refundgoods(items.id)"
            >待退货</van-button>

            <van-button
              size="mini"
              v-if="items.status < 0"
              type="danger"
              @click="refundinfo(items.id)"
            >退货详情</van-button>

        </template>
      </van-card>
    </van-tab>
  </van-tabs>
</template>
<script setup>
const { cookies } = useCookies();
const router = useRouter();
const LoginUser = reactive(cookies.get("LoginUser"));
const status = ref([
  {
    id: "",
    title: "全部",
  },
  {
    id: '0',
    title: "未支付",
  },
  {
    id: '1',
    title: "未发货",
  },
  {
    id: '2',
    title: "未收货",
  },
  {
    id: '3',
    title: "未评价",
  },
  {
    id: '4',
    title: "已完结",
  },
  {
    id: '-1',
    title: "售后中",
  },
  {
    id: '-4',
    title: "售后已结",
  },
  
]);
const active = ref("");
const orderdata = ref([]);
onBeforeMount(() => {
  getorderData();
});

const goBack = () => {
  router.push("/business/base/index");
};

const getorderData = async () => {
  let data = {
    busid: LoginUser.id,
    status: active.value,
  };

  let result = await Api.orderIndex(data);
  if (result.code == 1) {
    orderdata.value = result.data;
  }
};

const toInfoPage = (id) => {
  router.push({
    path: "/business/order/info",
    query: {
      orderid: id,
    },
  });
};

const toExpressPage = (id) => {
  router.push({
    path: "/business/order/express",
    query: {
      orderid: id,
    },
  });
};

const toEvaluationPage = (id) => {
  router.push({
    path: "/business/order/evaluation",
    query: {
      orderid: id,
    },
  });
};

const Confirmreceipt = (id) => {
  showConfirmDialog({
    title: "确认收货",
    message: "确认是否收货？",
  })
    .then(async () => {
      let data = {
        busid: LoginUser.id,
        orderid: id,
      };
      let result = await Api.orderReceipt(data);
      if (result.code == 1) {
        showNotify({
          type: "success",
          message: result.msg,
          onClose: () => {
            getorderData();
          },
        });
      } else {
        showNotify({ type: "danger", message: result.msg });
      }
    })
    .catch(() => {});
};

// 确认支付
const ConfirmPayment = (id) =>{
  showConfirmDialog({
    title: "确认支付",
    message: "确认是否支付？",
  })
    .then(async () => {
      let data = {
        busid: LoginUser.id,
        orderid: id,
      };
      let result = await Api.orderPayment(data);
      if (result.code == 1) {
        showNotify({
          type: "success",
          message: result.msg,
          onClose: () => {
            getorderData();
          },
        });
      } else {
        showNotify({ type: "danger", message: result.msg });
      }
    })
    .catch(() => {});
}

// 申请退货
const rejected = (id) => {
  router.push({
    path: "/business/order/refund",
    query: {
      orderid: id,
    },
  });
};

// 待退货
const refundgoods = (id) => {
  router.push({
    path: "/business/order/refundgoods",
    query: {
      orderid: id,
    },
  });
}

//退货详情
const refundinfo = (id) => {
  router.push({
    path: "/business/order/refundinfo",
    query: {
      orderid: id,
    },
  });
}
</script>
